<%--                                                                       --%>
<%-- Copyright (c) 2004 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<%@ include file="/mt/common/pickerPopupHelper.jsp" %>

<script language="JavaScript">
  
  function doCancelAction(myform) {
    var ok = true;
    //if (checkAnyChange(myform) == true) {
    //  ok = confirm('<bean-el:message key="confirmCancel" bundle="ep.messages"/>');
    //}
    if (ok && myform != null) {
      myform.pageTypeId.value = <c:out value="${epPageTypeEditForm.map.id}"/>;
      var goBackAction = '<c:out value="${param.goBackAction}"/>';
      if (goBackAction != '') {
        myform.action = '<c:url value="${param.goBackAction}"/>';
      }
      else {
        if (myform.pageTypeId.value == 0) {
          myform.action = '<c:url value="/ep/pageTypeSearch.do"/>';
        }
        else {
          myform.action = '<c:url value="/ep/pageTypeView.do"/>';
        }
      }
      myform.submit();
    }
  }

  function doResetAction(myform) {
    if (checkAnyChange(myform) == true) {
      myform.reset();
      doIncludeNavigationBlock(myform);
    }
  }

  function checkAnyChange(myform) {
    trimTextFields(myform);
    if (myform.name.value != myform.name.defaultValue) {
      return true;
    }
    if (myform.defaultTabName.value != myform.defaultTabName.defaultValue) {
      return true;
    }
    if (myform.displayTemplateId.value != <c:out value="${epPageTypeEditForm.map.displayTemplateId}"/> &&
      (myform.id.value != 0 || myform.displayTemplateId.selectedIndex != 0)) {
      return true;
    }
    if (myform.description.value != myform.description.defaultValue) {
      return true;
    }
    if (myform.includeSearchBlock.checked != myform.includeSearchBlock.defaultChecked) {
      return true;
    }
    if (myform.includeNavigationBlock.checked != myform.includeNavigationBlock.defaultChecked) {
      return true;
    }
    if (myform.parentChannelId.value != <c:out value="${epPageTypeEditForm.map.parentChannelId}"/>) {
      return true;
    }
    return false;
  }
  
  function trimTextFields(myform) {
    var theName = myform.name.value;
    myform.name.value = theName.stringTrim();
    var theDefaultTabName = myform.defaultTabName.value;
    myform.defaultTabName.value = theDefaultTabName.stringTrim();
    var theDescription = myform.description.value;
    myform.description.value = theDescription.stringTrim();
  }
  
  function doValidation(myform) {
    trimTextFields(myform);
    if (validatePageType(myform) == false) {
      return false;
    }
    var displayTemplateId = myform.displayTemplateId;
    if (displayTemplateId.value <= 0) {
      alert('<bean-el:message key="ep.page.type.error.invalidDisplayTemplate" bundle="ep.messages"/>');
      myform.displayTemplateId.focus();
      return false;
    }
    var includeNavigationBlock = myform.includeNavigationBlock;
    var parentChannelId = myform.parentChannelId;
    if (includeNavigationBlock.checked == true && parentChannelId.value >= 0) {
      alert('<bean-el:message key="ep.page.type.error.invalidParentChannel" bundle="ep.messages"/>');
      myform.actionSelectChannelRoot.focus();
      return false;
    }
    return true;
  }

  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

  function doIncludeNavigationBlock(myform) {
    var elminb = myform.includeNavigationBlock;
    var elmpc = myform.parentChannel;
    var elmscrbtn = myform.actionSelectChannelRoot;
    if (elminb.checked == false) {
      elmpc.style.visibility = 'hidden';
      elmpc.style.display = 'none';
      elmscrbtn.style.visibility = 'hidden';
      elmscrbtn.style.display = 'none';
    }
    else {
      elmpc.style.visibility = 'visible';
      elmpc.style.display = '';
      elmscrbtn.style.visibility = 'visible';
      elmscrbtn.style.display = '';
    }
    return true;
  }

  function doChannelSelectAction(myform) {
    doPickerPopupAction('<c:url value="/mt/categoryPickerPopup.do"/>', 
      'setParentChannel', 301, null, myform.parentChannelId.value, null, true, true, false,
      null, null, null, false);
  }
  
  function setParentChannel(ids, names) {
    if (ids.length >= 1 && names.length >= 1) {
      document.epPageTypeEditForm.parentChannelId.value = ids[0];
      document.epPageTypeEditForm.parentChannel.value = names[0];
    }
  }

</script>

<% 
  java.util.HashMap map = new java.util.HashMap();
  String ptOid = request.getParameter("pageTypeId");
  org.apache.struts.action.DynaActionForm theForm = (org.apache.struts.action.DynaActionForm)request.getAttribute("epPageTypeEditForm");
  Long id = (Long)theForm.get("id");
  if (id.longValue() > 0) {
    map.put("pageTypeId", id);
  }
  else if (ptOid != null && ptOid.length() > 0) {
    map.put("pageTypeId", ptOid);
  }
  else {
    map.put("pageTypeId", "-1");
  }
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

<html-el:javascript formName="/ep/pageTypeSave" method="validatePageType"/>
<html-el:form action="/ep/pageTypeSave.do" focus="name" onsubmit="return doValidation(epPageTypeEditForm);">
<input type="hidden" name="pageTypeId" value="<c:out value='${epPageTypeEditForm.map.id}'/>"/>
<html-el:hidden property="id"/>
<html-el:hidden property="parentChannelId"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
<c:choose>
  <c:when test="${epPageTypeEditForm.map.id == 0}">
        <bean-el:message key="ep.page.type.create.page.title" bundle="ep.messages"/>
  </c:when>
  <c:otherwise>
        <bean-el:message key="ep.page.type.edit.page.title" arg0="${epPageTypeEditForm.map.name}" bundle="ep.messages"/>
  </c:otherwise>
</c:choose> 
      </div>
<c:if test="${saveMessage == 'true'}">
      <div class="pageInstructions"><bean-el:message key="ep.page.types.update.message" bundle="ep.messages"/></div>
</c:if>
      <div class="errorMessage"><html-el:errors bundle="ep.messages"/></div>
      <%@ include file="/mc/common/pagetab.jsp" %>
      <div class="pageSubtitle">
        <span class="left">
          <bean-el:message key="ep.page.type.information" bundle="ep.messages"/>
        </span>
        <span class="right">&nbsp;</span>
      </div> 
      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="<bean-el:message key='ep.page.types' bundle="ep.messages"/>">
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.name" bundle="ep.messages"/>:*
          </td>
          <td class="labelField" width="70%">
            <html-el:text name="epPageTypeEditForm" property="name" size="30" maxlength="80"/> 
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.display.name" bundle="ep.messages"/>:*
          </td>
          <td class="labelField" width="70%">
            <html-el:text name="epPageTypeEditForm" property="defaultTabName" size="20" maxlength="40"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.display.template" bundle="ep.messages"/>:*
          </td>
          <td class="labelField" width="70%">
            <html-el:select name="epPageTypeEditForm" property="displayTemplateId" size="1">
              <html-el:optionsCollection name="epPageTypeEditForm" property="displayTemplateIdList"/>
            </html-el:select>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.description" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <html-el:textarea name="epPageTypeEditForm" property="description" rows="4" cols="50"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.required.blocks.entries" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <html-el:text name="epPageTypeEditForm" property="numberOfEntries" size="3" maxlength="3" onkeypress="return checkNumberOnly(event)"/>
          </td>
        </tr>  
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.include.search.block" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <html-el:checkbox name="epPageTypeEditForm" property="includeSearchBlock"/>
          </td>
        </tr>
        <tr> 
          <td class="label" width="30%">
            <bean-el:message key="ep.page.type.include.navigation.block" bundle="ep.messages"/>:
          </td>
          <td class="labelField" width="70%">
            <html-el:checkbox name="epPageTypeEditForm" property="includeNavigationBlock"
              onclick="return doIncludeNavigationBlock(this.form);"/>
            &nbsp;
            <html-el:text name="epPageTypeEditForm" property="parentChannel" size="20" maxlength="40" disabled="true"/>
            &nbsp;
            <input type="button" name="actionSelectChannelRoot" class="pageButton"
              onclick="doChannelSelectAction(this.form);"
              value='<bean-el:message key="button.select.channel" bundle="ep.messages"/>'>
          </td>
        </tr>
      </table>
      <div class="pageButtonWrap" align="right">
        <input type="button" name="actionReset" class="pageButton" 
          onclick="doResetAction(this.form);"
          value='<bean-el:message key="button.reset" bundle="ep.messages"/>'>
        &nbsp;&nbsp;
        <input type="button" name="actionCancel" class="pageButton" 
          onclick="doCancelAction(this.form);"
          value='<bean-el:message key="button.cancel" bundle="ep.messages"/>'>
        <input type="submit" name="actionSaveContinue" class="pageButton"
          value='<bean-el:message key="button.save" bundle="ep.messages"/>'>
      </div>
    </td> 
  </tr>
</table>
<script language="Javascript1.2">
  var inb = <c:out value="${epPageTypeEditForm.map.includeNavigationBlock}"/>;
  if (inb != null && inb == false) {
    var theform = document.forms['epPageTypeEditForm'];
    theform.parentChannel.style.visibility = 'hidden';
    theform.parentChannel.style.display = 'none';
    theform.actionSelectChannelRoot.style.visibility = 'hidden';
    theform.actionSelectChannelRoot.style.display = 'none';
  }
</script>
</html-el:form> 
